package thread.threadlocal;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * @program: ch-general
 * @description: 线程共享变量
 * @author: cuihao
 * @create: 2019-05-28 13:22
 **/
public class MyThreadLocal {


    // ThreadLocalMap<> 为thread内部的一个结构，key 为ThreadLocal
    // ThreadLocal 的get方法. 会调用Thread.currentThread获取当前Thread，再获取ThreadLocalMap,在传入ThreadLocal作为key获取value






    private static final ThreadLocal<AtomicInteger> threadLocal = new ThreadLocal<AtomicInteger>() {
        /**
         * ThreadLocal没有被当前线程赋值时或当前线程刚调用remove方法后调用get方法，返回此方法值
         */
        @Override
        protected AtomicInteger initialValue() {
            System.out.println("调用get方法时，当前线程共享变量没有设置，调用initialValue获取默认值！");
            return null;
        }
    };

    public static void main(String[] args) {
        new Thread(new MyIntegerTask("IntegerTask1")).start();
        //new Thread(new MyStringTask("StringTask1")).start();
        new Thread(new MyIntegerTask("IntegerTask2")).start();
        //new Thread(new MyStringTask("StringTask2")).start();
    }

    public static class MyIntegerTask implements Runnable {
        private final String name;

        MyIntegerTask(String name) {
            this.name = name;
        }

        @Override
        public void run() {
            for (int i = 0; i < 6; i++) {
                // ThreadLocal.get方法获取线程变量
                if (null == MyThreadLocal.threadLocal.get()) {
                    // ThreadLocal.et方法设置线程变量
                    MyThreadLocal.threadLocal.set(new AtomicInteger(0));
                    System.out.println("线程" + name + ": 0");
                } else {
                    AtomicInteger num = MyThreadLocal.threadLocal.get();
                    num.incrementAndGet();
//                    MyThreadLocal.threadLocal.set(num + 1);
                    System.out.println("线程" + name + ": " + MyThreadLocal.threadLocal.get());
                    if (name.equals("IntegerTask2") && num.get() == 2) {
                        MyThreadLocal.threadLocal.remove();
                    }
                }
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }

    }

//    public static class MyStringTask implements Runnable {
//        private final String name;
//
//        MyStringTask(String name) {
//            this.name = name;
//        }
//
//        @Override
//        public void run() {
//            for (int i = 0; i < 5; i++) {
//                if (null == MyThreadLocal.threadLocal.get()) {
//                    MyThreadLocal.threadLocal.set("a");
//                    System.out.println("线程" + name + ": a");
//                } else {
//                    String str = (String) MyThreadLocal.threadLocal.get();
//                    MyThreadLocal.threadLocal.set(str + "a");
//                    System.out.println("线程" + name + ": " + MyThreadLocal.threadLocal.get());
//                }
//                try {
//                    Thread.sleep(800);
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
//            }
//        }
//    }
}